package org.springblade.modules.admin.service.impl;

import com.aliyun.oss.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springblade.core.mp.base.BaseEntity;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.RobotAddDTO;
import org.springblade.modules.admin.entity.Robot;
import org.springblade.modules.admin.mapper.RobotMapper;
import org.springblade.modules.admin.service.IRobotService;
import org.springblade.modules.admin.vo.RobotCountVO;
import org.springblade.modules.admin.vo.RobotVO;
import org.springblade.modules.system.entity.Tenant;
import org.springblade.modules.system.service.ITenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Sharry
 * @since 2023/5/17
 */
@Service
public class RobotServiceImpl extends BaseServiceImpl<RobotMapper, Robot> implements IRobotService {

	@Autowired
	private ITenantService tenantService;

	@Override
	public List<RobotVO> getListByCondition(String robotId, String tenantId) {

		List<RobotVO> result = baseMapper.getListByConditionOut(robotId, tenantId);
		for (RobotVO robotVO : result) {
			RobotCountVO robotCountVO =  baseMapper.getRobotCOuntByRobotId(robotVO.getRobotId());
			// 统计结果赋值
			robotVO.setBuildingNum(robotCountVO.getBuildingNum());
			robotVO.setElevatorNum(robotCountVO.getElevatorNum());
			robotVO.setGroupNum(robotCountVO.getGroupNum());
			Integer buildingNum = robotVO.getBuildingNum();
			Integer elevatorNum = robotVO.getElevatorNum();
			String byTenantId = robotVO.getTenantId();
			Tenant t = tenantService.getByTenantId(byTenantId);
			String tenantName = t.getTenantName();
			String tenantFullName = tenantName + "/" + byTenantId;
			robotVO.setTenantNameFull(tenantFullName);
			robotVO.setProjectElevatorsNum(buildingNum + "/" + elevatorNum);
		}
		return result;
	}

	@Override
	public boolean deleteRelationship(String robotId) {
		// 1. 验证 该机器人是否 已与 群-项目-电梯绑定
		List<RobotVO> result = baseMapper.getListByCondition(robotId, null);
		for (RobotVO robotVO : result) {
			Integer buildingNum = robotVO.getBuildingNum();
			Integer elevatorNum = robotVO.getElevatorNum();
			Integer groupNum = robotVO.getGroupNum();
			if(0 < buildingNum || 0 < elevatorNum || 0 < groupNum){
				throw new ServiceException("当前机器人已绑定租户微信群的项目电梯，无法删除，请前往机器人绑定租户解除微信群与项目电梯关系，再删除");
			}
		}
		// 2. 解绑
		Robot one = getOne(new LambdaQueryWrapper<Robot>().eq(Robot::getRobotId, robotId));
		return update(one,new LambdaUpdateWrapper<Robot>().set(Robot::getTenantId,"").set(BaseEntity::getIsDeleted,null).eq(Robot::getRobotId,robotId));
	}

	@Override
	public boolean edit(String robotId, RobotAddDTO robotAddDTO) {
		// 1. 验证 该机器人是否 已与 群-项目-电梯绑定
		List<RobotVO> result = baseMapper.getListByCondition(robotId, null);
		for (RobotVO robotVO : result) {
			Integer buildingNum = robotVO.getBuildingNum();
			Integer elevatorNum = robotVO.getElevatorNum();
			Integer groupNum = robotVO.getGroupNum();
			if(0 < buildingNum || 0 < elevatorNum || 0 < groupNum){
				throw new ServiceException("当前机器人已绑定租户微信群的项目电梯，无法编辑，请前往机器人绑定租户解除微信群与项目电梯关系，再编辑");
			}

			String tenantId = robotVO.getTenantId();
			List<Robot> list = list(new LambdaQueryWrapper<Robot>().eq(Robot::getTenantId, tenantId));
			if(!Func.isEmpty(list)){
				throw new ServiceException("当前租户已有绑定机器人");
			}
		}
		// 2. 更新
		Robot one = getOne(new LambdaQueryWrapper<Robot>().eq(Robot::getRobotId, robotId));
		one.setTenantId(robotAddDTO.getTenantId());
		one.setRobotId(robotAddDTO.getRobotId());
		one.setRobotQrcode(robotAddDTO.getRobotQrCode());
		return updateById(one);
	}
}
